﻿@page "/"
@rendermode InteractiveWebAssembly
@inject IRepository PizzaStore
@inject OrderState OrderState
@inject NavigationManager NavigationManager


<PageTitle>Blazing Pizzas</PageTitle>

<div class="main">
	<ul class="pizza-cards">
		@if (specials is not null)
		{
			@foreach (var special in specials)
			{
				<li @onclick="@(()=>OrderState.ShowConfigurePizzaDialog(special))" style="background-image: url('@special.ImageUrl')">
					<div class="pizza-info">
						<span class="title">@special.Name</span>
						@special.Description
						<span class="price">@special.GetFormattedBasePrice()</span>
					</div>
				</li>
			}
		}
	</ul>
</div>

<div class="sidebar">
	@if (OrderState.Order.Pizzas.Any())
	{
		<div class="order-contents">
			<h2>Your order</h2>

			@foreach (var configuredPizza in OrderState.Order.Pizzas)
			{
				<ConfiguredPizzaItem Pizza="configuredPizza" OnRemoved="@(() => OrderState.RemoveConfiguredPizza(configuredPizza))" />
			}
		</div>
	}
	else
	{
		<div class="empty-cart">Choose a pizza<br>to get started</div>
	}

	<div class="order-total @(OrderState.Order.Pizzas.Any() ? "" : "hidden")">
		Total:
		<span class="total-price">@OrderState.Order.GetFormattedTotalPrice()</span>
		<button class="btn btn-warning" disabled="@(OrderState.Order.Pizzas.Count == 0)" @onclick="PlaceOrder">
			Order >
		</button>
	</div>
</div>

@if (OrderState.ShowingConfigureDialog)
{
	<ConfigurePizzaDialog Pizza="OrderState.ConfiguringPizza"
												OnCancel="OrderState.CancelConfigurePizzaDialog"
												OnConfirm="OrderState.ConfirmConfigurePizzaDialog" />
}

@code {
	List<PizzaSpecial>? specials;

	protected override async Task OnInitializedAsync()
	{
		specials = await PizzaStore.GetSpecials();
	}

	async Task PlaceOrder()
	{
		var newOrderId = await PizzaStore.PlaceOrder(OrderState.Order);
		OrderState.ResetOrder();
		NavigationManager.NavigateTo($"myorders/{newOrderId}");
	}

}